﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Text;
using HtmlAgilityPack;
using System.Collections.Generic;
using System.Diagnostics;

namespace depl
{
    public class StringUtil
    {
        static char[] replacement = { 'a', 'o', 'u', 's', 'a', 'c', 'e', 'l', 'n', 'o', 's', 'z', 'z' };
        static char[] accents =     { 'ä', 'ö', 'ü', 'ß', 'ą', 'ć', 'ę', 'ł', 'ń', 'ó', 'ś', 'ż', 'ź' };

        static string[] ignorelist = { "ein", "eine", "einer", "einem", "hat","der", "die", "das", "sich", "mich", "(nur Singular)" };
        static char[] delimiters = new char[] { ',', ';', '(', ')' };
        static StringBuilder sbStripAccents = new StringBuilder();

         public static string RemoveDiacritics(string accentedStr)
         {
             accentedStr = accentedStr.ToLower();
               if (accentedStr.IndexOfAny(accents) > -1)
               {
                  sbStripAccents.Length = 0;
                  sbStripAccents.Append(accentedStr);
                  for (int i = 0; i < accents.Length; i++)
                  {
                     sbStripAccents.Replace(accents[i], replacement[i]);
                  }
 
                  return sbStripAccents.ToString();
               }
               else
                  return accentedStr;
         }

         public static char RemoveDiacritics(char inputChar)
         {
             inputChar = Char.ToLower(inputChar);
             for (int i = 0; i < accents.Length; i++ )
             {
                 if (accents[i] == inputChar)
                     return replacement[i];
             }
             return inputChar;
         }

         public static string RemoveBlanks(string str)
         {
             StringBuilder sb = new StringBuilder(str.Length);
             for (int i = 0; i < str.Length; i++)
             {
                 char c = str[i];
                 switch (c)
                 {
                     case '\r':
                     case '\n':
                     case '\t':
                     case ' ':
                         continue;
                     default:
                         sb.Append(c);
                         break;
                 }
             }
             return sb.ToString();
         }

         public static void ExtractTranslation(string str, out string outkeywords, out List<string> outtranslations)
        {
            bool entryNamePassed = false;
            HtmlDocument doc = new HtmlDocument();
            doc.LoadHtml(str);
            List<string> translations = new List<string>();
            StringBuilder keywords = new StringBuilder();
            foreach (HtmlNode node in doc.DocumentNode.ChildNodes)
            {
                switch (node.Name)
                {
                    case "b":
                        if(node.InnerHtml.StartsWith("<a href="))
                            break;

                        string[] split = node.InnerText.Split(',');
                        foreach (string s in split)
                        {
                            string translation = s.TrimStart(' ');
                            if ((translation != "") && (translation[0] >= 'A'))
                            {
                                translations.Add(translation);
                            }
                        }
                        break;
                    case "#text":
                        if (!entryNamePassed)
                        {
                            entryNamePassed = true;
                            break;
                        }
                        //string filteredTxt = node.InnerText.Replace(@",", string.Empty);
                        //filteredTxt = filteredTxt.Replace("(PL die", string.Empty);
                        //filteredTxt = filteredTxt.Replace(")", string.Empty);

                        split = node.InnerText.Split(delimiters);
                        //Debug.WriteLine("keywords");
                        string filtereds;
                        foreach (string s in split)
                        {
                            if (s == "" || s==" ")
                                continue;
                            filtereds = s;
                            filtereds = filtereds.Replace("PL", String.Empty);
                            filtereds = filtereds.Replace("der", String.Empty);
                            filtereds = filtereds.Replace("die", String.Empty);
                            filtereds = filtereds.Replace("das", String.Empty);
                            filtereds = filtereds.Replace("hat", String.Empty);
                            filtereds = filtereds.Replace("nur Singular", String.Empty);
                            
                            filtereds = filtereds.Trim(' ');
                            filtereds = filtereds.Replace(@"/", " ");

                            if (filtereds != "")
                            {
                                keywords.Append(filtereds);
                                keywords.Append(' ');
                            }
                            
                            
                            /*string[] split2 = s.Split(' ');
                            foreach (string keyword in split2)
                            {
                                string key = keyword.TrimStart(' ');
                                //ignorelist.
                                if (key != "" && key.Length > 1)
                                {
                                    //Debug.WriteLine(keyword);
                                    keywords.Append(keyword);
                                }
                            }*/
                            
                        }
                        
                        break;
                    default:
                        //Debug.WriteLine("normal: " + node.InnerText);

                        break;
                }
            }
             
            string finalstring = keywords.ToString();
            finalstring = RemoveDiacritics(finalstring);
            outkeywords = finalstring;
            outtranslations = translations;
        }

            

    }

}
